﻿using System;
using System.Threading.Tasks;
using Orleans.Configuration;
using Orleans.IGrains;

namespace Orleans.Client
{
    /// <summary>
    /// 连接客户端
    /// </summary>
    public class Program
    {
        private static IClusterClient client;

        public static async Task Main(string[] args)
        {
            client = await ConnectClient();
            for (int i = 0; i < 100; i++)
            {
                await GetEventGrainAndUpdate(i);
            }
            
            return;
        }

        /// <summary>
        /// 获取事件对像,，执行修改
        /// </summary>
        /// <returns></returns>
        private static async Task GetEventGrainAndUpdate(int id)
        {
            var eventGrain = client.GetGrain<IEvent>(id);
            await eventGrain.Update();
        }

        /// <summary>
        /// 使用本地配置连接客户端服务
        /// </summary>
        /// <returns></returns>
        private static async Task<IClusterClient> ConnectClient()
        {
            ClientBuilder builder = new ClientBuilder();
            //配置客户端以连接到本地主机上的筒仓。
            builder.UseLocalhostClustering();
            builder.Configure<ClusterOptions>(options =>
            {
                options.ClusterId = "dev";
                options.ServiceId = "MyHost";
            });
            //创建客户端
            IClusterClient client = builder.Build();
            await client.Connect();
            return client;
        }
    }
}
